//
//  GQHSettingsView.m
//  Xanthium
//
//  Created by GuanQinghao on 01/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHSettingsView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"
#import "UIView+YeeBadge.h"

@interface GQHSettingsView () <UITableViewDelegate,UITableViewDataSource> {
    
    NSArray *titleArray;
}

@end


@implementation GQHSettingsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    titleArray = @[@"头像",@"修改手机号",@"修改密码", @"检测更新",@"关于我们",@"退出登录"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _qh_tableView.separatorColor = kLightGrayColor;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    _qh_tableView.tableFooterView = [UIView new];
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.row) {
        
        return 60.0f;
    }
    
    // 账户类型 第三方登录没有修改手机号和修改密码
    BOOL accountType = [[NSUserDefaults standardUserDefaults] boolForKey:@"accountType"];
    if ((1 == indexPath.row || 2 == indexPath.row) && accountType) {
        
        return CGFLOAT_MIN;
    }
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.row) {
        
        static NSString *identifier = @"aCell";
        GQHTruckImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHTruckImagesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        
        cell.qh_captionLabel.text = titleArray[indexPath.row];
        cell.qh_captionLabel.textColor = kDarkBlackColor;
        /// 头像
        NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:USER_GET_AVATAR];
        NSURL *URL = [NSURL URLWithString:URLString];
        [cell.qh_rightImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
        cell.qh_leftImageView.hidden = YES;

        return cell;
    } else {
        
        static NSString *identifier = @"bCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        cell.textLabel.text = titleArray[indexPath.row];
        
        // 账户类型 第三方登录没有修改手机号和修改密码
        BOOL accountType = [[NSUserDefaults standardUserDefaults] boolForKey:@"accountType"];
        if ((1 == indexPath.row || 2 == indexPath.row) && accountType) {
            
            cell.hidden = YES;
        }
        if (indexPath.row == 3)
        {
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"V%@", [infoDictionary objectForKey:@"CFBundleShortVersionString"]];
            
            if (self.appVersion && ![self.appVersion isEqualToString:[infoDictionary objectForKey:@"CFBundleShortVersionString"]])
            {
                [cell.textLabel ShowBadgeView];
            }
            else
            {
                [cell.textLabel hideBadgeView];
            }
        }
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 3)
    {
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        if (self.appVersion && ![self.appVersion isEqualToString:[infoDictionary objectForKey:@"CFBundleShortVersionString"]])
        {
            [self showAlertWithTitle:@"温馨提醒" message:@"当前版本较低,请前往更新最版本!" confirmTitle:@"确定" confirmHandler:^(UIAlertAction *confirmAction) {
                
                NSString *str = @"itms-apps://itunes.apple.com/cn/app/id1329918420?mt=8"; //更换id即可
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            }];
        }
        else
        {
            [self showToastWithText:@"当前版本已是最新版本!"];
        }
    }
    else
    {
        if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
            
            [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

@end
